package com.ctg.payroll2.service.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.ctg.payroll2.bean.po.PbCity;
import com.ctg.payroll2.bean.po.PbProvince;
import com.ctg.payroll2.dao.PbCityDAO;
import com.ctg.payroll2.dao.PbProvinceDAO;
import com.ctg.payroll2.service.ProvinceCityService;
import com.ctg.platform.util.Page;
import com.ctg.platform.util.Parse;

public class CityServiceImpl implements ProvinceCityService {

	private PbCityDAO pbCityDAO;

	private PbProvinceDAO pbProvinceDAO;

	public PbCityDAO getPbCityDAO() {
		return pbCityDAO;
	}

	public void setPbCityDAO(PbCityDAO pbCityDAO) {
		this.pbCityDAO = pbCityDAO;
	}

	public PbProvinceDAO getPbProvinceDAO() {
		return pbProvinceDAO;
	}

	public void setPbProvinceDAO(PbProvinceDAO pbProvinceDAO) {
		this.pbProvinceDAO = pbProvinceDAO;
	}

	public Page getProvince(Map parameters, int n, int m) {
		return pbCityDAO.pagedQuery(parameters, n, m);
	}

	public void insertByJson(String province) {

	}

	public void updateByJson(String city) {

		/*
		 * {"province":{"province_id":"10711","province_name":"山西省","province_code":"34","province_pinyin":"shangxi","province_abbr_pinyin":"sx"}}
		 * JSONObject["province"] is not a JSONArray.
		 */
		try {
			JSONObject jsonObject;
			jsonObject = JSONObject.fromObject(city);
			// System.out.println(city);

			PbCity entry = null;
			JSONObject o = jsonObject.getJSONObject("city");

			// o = (JSONObject)modifiedItems.next();
			entry = new PbCity();
			entry.setCity_id(Parse.toInteger(o.getString("city_id")));
			entry.setProvince_id(Parse.toInteger(o.getString("province_id")));
			entry.setCity_name(o.getString("city_name"));
			entry.setCity_code(o.getString("city_code"));
			entry.setCity_abbr_pinyin(o.getString("city_abbr_pinyin"));
			entry.setCity_pinyin(o.getString("city_pinyin"));
			entry.setHave_used(1);

			if (entry.getCity_id() == null || entry.getCity_id().equals("")) {

				pbCityDAO.insert(entry);
			} else
				pbCityDAO.update(entry);

		} catch (Exception e) {
			e.printStackTrace();

		}

	}

	public Page getProvinceCity(Map parameters, int n, int m) {
		return pbCityDAO.pagedQuery(parameters, n, m);
	}

	public void saveProvinceCity(PbCity o) {
		if (o.getCity_id() == null) {
			o.setHave_used(1);
			pbCityDAO.insert(o);
		} else {
			pbCityDAO.update(o);
		}
	}

	public void deleteProvinceCity(List data) throws SQLException {

		pbCityDAO.delete(data);

	}

	public List query(Integer provinceId, String name) {
		HashMap parameters = new HashMap();
		if (provinceId != null)
			parameters.put("city_id", provinceId);

		List list = pbCityDAO.queryForList(".getAll", parameters);

		return list;
	}

	public List queryProvince() {
		HashMap parameters = new HashMap();

		// return pbCityDAO.pagedQuery(parameters, 0, 100);
		List list = pbProvinceDAO.queryForList(".getProvince", parameters);

		return list;
	}

	public void saveProvinceCity(PbProvince o) {
		// TODO Auto-generated method stub

	}

	public boolean deleteProvince(List data) throws SQLException {
		// TODO Auto-generated method stub
		return false;
	}

	/**
	 * 省市级联：通过省的id查询城市
	 * @param province_id
	 * @return
	 */
	public List queryCityByProviceId(int province_id) {
		HashMap parameters = new HashMap();
		parameters.put("province_id", province_id);
		List list = null;
		try {
		list = pbCityDAO.queryForList(".queryCityByProviceId", parameters);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}
}
